<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../base.jsp" %>
<!-- Select2 -->
<style>
	.content-wrapper{margin-left: 0px}
</style>
<body class="hold-transition skin-blue sidebar-mini">

	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>规则管理<small>新增/编辑规则</small></h1>
				<ol class="breadcrumb">
					<li><a href="javascript:;"><i class="fa fa-dashboard"></i> 首页</a></li>
					<li class="active">新增/编辑规则</li>
				</ol>
			</section>
			<!-- Main content -->
			<section class="content">
	            <!-- form start -->
				<form id="saveForm">
				<div class="row">
					<div class="col-md-12">
					<div class="box box-info">
			            <!-- /.box-header -->
			              <div class="box-body form-horizontal">
			              	<div class="form-group">
			                  <label for="inputEmail3" class="col-sm-2 control-label">规则名称</label>
			                  <div class="col-sm-3">
			                    <input type="text" class="form-control" name="rulename" id="rulename" placeholder="规则名称">
			                    <input type="hidden" name="id" id="id">
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword3" class="col-sm-2 control-label">日志示例</label>
			                  <div class="col-sm-3">
			                    <textarea class="form-control" rows="5" placeholder="日志示例"></textarea>
			                  </div>
			                </div>
			              	<div class="form-group">
			                  <label for="inputEmail3" class="col-sm-2 control-label">添加解析规则</label>
			                  <div class="col-sm-3">
			                    <select name="ruletype" id="ruletype" class="form-control select2">
										<option value="">==全部==</option>
										<option value="reg">正则解析</option>
										<option value="xml">XML解析</option>
									</select>
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword3" class="col-sm-2 control-label">规则内容</label>
			                  <div class="col-sm-3">
			                    <textarea class="form-control" name="rulecontent" id="rulecontent"  rows="5" placeholder="正则表达式"></textarea>
			                  </div>
			                </div>
			                
			                <div class="box-footer">
				                <a href="javascript:;" class="btn btn-info test">测试</a>
				                <a href="javascript:;" class="btn btn-info save">提交</a>
				                <a href="${url }serverLog/list" class="btn btn-default">取消</a>
				             </div>
				              <!-- /.box-body -->
			          	</div>
			          </div>
			        </div>
				</form>
			</section>
		</div>
	</div>
	<div id="result" style="display: none">${result}</div>
	
	<script src="${url }bootstrap/js/jquery.validate.min.js"></script>
	<script type="text/javascript">
	$(function () {
		var result = $('#result').html();
		if(result){
			result = eval( "(" + result + ")" );
			
			var content = result.result.content;
			if(content){
				for(var i=0;i<content.length;i++){
					$('#id').val(content[i].id);
					$('#rulename').val(content[i].name);
					$('#ruletype').val(content[i].ruletype);
					$('#rulecontent').val(content[i].rulecontent);
				} 
			}
		}
		
 		
		$('.save').on('click',function(){
			if($('#saveForm').valid()){
				$.ajax({
					type:"POST",
					url: path+'serverLog/add',
					data:$('#saveForm').serialize(),
					success: function(data){
						console.log(data.result.exec);
						if(data.result.exec){
							layer.alert('保存成功', {
								  closeBtn: 0
							}, function(){
								location.href = path+'serverLog/list';
							});
						}else{
							layer.alert("保存失败");
						}
					}
				});
			}
		});
	});
	
	$("#saveForm").validate({
	    rules: {
	    	name: {
			 required: true,
			 maxlenth:50
			},
			ruletype:'required',
			rulecontent:'required',
	    },
	    messages: {
	    	name: {
			 required: '请输入规则名称',
			 maxlenth:'规则名称不能超过50个字符'
			},
			ruletype: '请选择解析规则类型',
			rulecontent:'请输入规则内容'
	    }
	});
	</script>
	
	<style>
		.error{color:red;font-weight: 500;}
	</style>
</body>